//
//  HistoryViewController.m
//  MyWallet
//
//  Created by 美鑫科技 on 16/8/5.
//  Copyright © 2016年 MorpLCP. All rights reserved.
//

#import "HistoryViewController.h"
#import "HistoryDetailViewController.h"

@interface HistoryViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *dataDict;

@end

@implementation HistoryViewController

static NSString *cellIdent = @"historylistcell";

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubViews];
    [self analyticalData];
}

- (void)initSubViews
{
    self.title = @"历史账单";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(
                                                                   0,
                                                                   0,
                                                                   ScreenWidth,
                                                                   ScreenHeight
                                                                   ) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdent];
    [self.view addSubview:_tableView];
}

- (void)analyticalData
{
    [[BillManager manager] getPaymentList:^(NSDictionary *list)
    {
        self.dataDict = list.mutableCopy;
        [self.tableView reloadData];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataDict.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataDict[self.dataDict.allKeys[section]] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *titleStr = [NSString stringWithFormat:@"%@年", self.dataDict.allKeys[section]];
    return titleStr;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent forIndexPath:indexPath];
    NSArray *keyArray = self.dataDict.allKeys;
    NSArray *monthArray = self.dataDict[keyArray[indexPath.section]];
    cell.textLabel.text = [NSString stringWithFormat:@"%@月份", monthArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryDetailViewController *histDVC = [HistoryDetailViewController new];
    NSArray *keys = self.dataDict.allKeys;
    NSString *year = keys[indexPath.section];
    NSArray *dateArray = self.dataDict[year];
    NSString *month = dateArray[indexPath.row];
    NSString *date = [NSString stringWithFormat:@"%@-0%@", year, month];
    histDVC.dataDict = [[BillManager manager] getBillListWithDate:date];
    histDVC.title = [NSString stringWithFormat:@"%@月份账单", month];
    [self.navigationController pushViewController:histDVC animated:YES];
}

- (NSMutableDictionary *)dataDict
{
    if (!_dataDict)
    {
        _dataDict = [NSMutableDictionary dictionary];
    }
    return _dataDict;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
